package com.zzptc.easyshop.db;

import com.sun.scenario.effect.impl.prism.PrImage;
import com.zzptc.easyshop.util.MyUtil;

import java.sql.*;
import java.util.Map;

public class MysqlDB implements IDB{
    private  Connection conn = null;
    private Map map;
    private String DRIVER;
    private String URL;
    private String UNAME;
    private String PASS;
    private String CHARACTER;

    @Override
    public Connection getConn() throws SQLException {
        try {
            map = MyUtil.readXML("db-config.xml");
            DRIVER = (String) map.get("driver");
            URL = (String) map.get("url");
            UNAME = (String) map.get("uname");
            PASS = (String)map.get("pwd");
            CHARACTER = (String)map.get("character");
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class.forName(DRIVER);
            //动态链接数据库
            conn = DriverManager.getConnection(URL+"&"+CHARACTER,UNAME,PASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }

    @Override
    public void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException {
    if (conn!=null&&!conn.isClosed()){
    conn.close();
    }
    if (ps!=null){
        ps.close();
    }
    if (rs!=null){
        rs.close();
    }
    }
}
